import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.LineString;
import org.locationtech.jts.geom.Polygon;

import java.io.Serializable;
import java.util.List;

public class PointVO implements Serializable {
    private static final long serialVersionUID = -8022567513952387168L;
    public double x;
    public double y;
    public double z;
    public PointVO() {
    }

    public PointVO(double x, double y, double z) {
        this.x = x;
        this.y = y;
        this.z = z;
    }

    public PointVO(double x, double y) {
        this.x = x;
        this.y = y;
    }

    public double getX() {
        return x;
    }

    public void setX(double x) {
        this.x = x;
    }

    public double getY() {
        return y;
    }

    public void setY(double y) {
        this.y = y;
    }

    public double getZ() {
        return z;
    }

    public void setZ(double z) {
        this.z = z;
    }
    /**
     * 通过 List<Point> 获取 Polygon
     */
    public static Polygon createPolygon(List<PointVO> points) {
        return GeometryUtil.factory.createPolygon(transCs(points));
    }

    public static LineString createLines(List<PointVO> points) {
        return GeometryUtil.factory.createLineString(transCs(points));
    }
    /**
     * 转化成Coordinate数组
     */
    public static Coordinate[] transCs(List<PointVO> points) {
        Coordinate[] coordinates = new Coordinate[points.size()];
        for (int i = 0; i < points.size(); i++) {
            coordinates[i] = new Coordinate(points.get(i).x, points.get(i).y);
        }
        return coordinates;
    }

    @Override
    public boolean equals(Object obj) {
        /** 对象是 null 直接返回 false **/
        if (obj == null) {
            return false;
        }
        /** 对象是当前对象，直接返回 true **/
        if (this == obj) {
            return true;
        }
        /** 判断对象类型是否是User **/
        if (obj instanceof PointVO) {
            PointVO vo = (PointVO) obj;
            /** 比较每个属性的值一致时才返回true **/
            /** 有几个对象就要比较几个属性 **/
            if (vo.x== this.x && vo.y==this.y&&vo.z==this.z) {
                return true;
            }
        }
        return false;
    }
    /**
     * 重写hashcode方法，返回的hashCode一样才再去比较每个属性的值
     */
    @Override
    public int hashCode() {
        return Double.toString(this.x).hashCode() * Double.toString(this.y).hashCode()* Double.toString(this.z).hashCode();
    }

    @Override
    public String toString() {
        return "(" + x + ", " + y + ")";
    }

}
